#coding=utf-8
import datetime, decimal
import wx
import wx.html

from db import *
from utility import *
import env
import wx.lib.pubsub as ps

status_list = ['draft', 'active', 'paused']

class AdPanel(wx.Panel):
    def __init__(self, parent, id=-1):
        wx.Panel.__init__(self, parent, id)
        
        '''
        label_value = u"Ad Information:" 
        if ad:
            label_value += self.ad.campaign.add()
        '''    
        
        topLbl = wx.StaticText(self, -1, label="Ad Information")
        topLbl.SetFont(wx.Font(11, wx.SWISS, wx.NORMAL, wx.BOLD))
        
        nameLbl = wx.StaticText(self, -1, "Name:")
        name = wx.TextCtrl(self, -1)
        self.name = name
        
        budgetLbl = wx.StaticText(self, -1, label="Budget:")
        budget = wx.TextCtrl(self, -1)
        h = budget.GetSize().height
        w = budget.GetSize().width + budget.GetPosition().x + 2
        budgetSpb = wx.SpinButton(self, -1,size=(h*2/3, h),style=wx.SP_VERTICAL)
        budgetSpb.SetRange(0, 500)
        budgetSpb.SetValue(0)
        self.budgetSpb = budgetSpb
        self.budget = budget
        self.Bind(wx.EVT_SPIN, self.OnSpin, budgetSpb)
        
        statusLbl = wx.StaticText(self, -1, label="Status:")
        status = wx.Choice(self, -1, choices = status_list)
        self.status = status
        
        headlineLbl = wx.StaticText(self, -1, "Headline:")
        headline = wx.TextCtrl(self, -1)
        self.headline = headline
        
        desc1Lbl = wx.StaticText(self, -1, "Description Line 1:")
        desc1 = wx.TextCtrl(self, -1)
        self.desc1 = desc1
        
        desc2Lbl = wx.StaticText(self, -1, "Description Line 2:")
        desc2 = wx.TextCtrl(self, -1)
        self.desc2 = desc2
        
        display_urlLbl = wx.StaticText(self, -1, "Dispaly Url:")
        display_url = wx.TextCtrl(self, -1)
        self.display_url = display_url
        
        destination_urlLbl = wx.StaticText(self, -1, "Destination Url:")
        destination_url = wx.TextCtrl(self, -1)
        self.destination_url = destination_url
        
        keywordsLbl = wx.StaticText(self, -1, 'Keyword(separated by ",")')
        keyword = wx.TextCtrl(self)
        
        self.keyword = keyword
        
        nega_keywordsLbl = wx.StaticText(self, -1, 'Negative Keyword:')
        nega_keyword = wx.TextCtrl(self)
        
        self.nega_keyword = nega_keyword
        
        self.Bind(wx.EVT_TEXT, self.OnText, headline)
        self.Bind(wx.EVT_TEXT, self.OnText, desc1)
        self.Bind(wx.EVT_TEXT, self.OnText, desc2)
        self.Bind(wx.EVT_TEXT, self.OnText, display_url)
        self.Bind(wx.EVT_TEXT, self.OnText, destination_url)

        preview = wx.html.HtmlWindow(self)
        if "gtk2" in wx.PlatformInfo:
            preview.SetStandardFonts()
        self.preview = preview

        applyBtn = wx.Button(self, -1, "Apply")
        cancelBtn = wx.Button(self, -1, "Cancel")
        applyaddBtn = wx.Button(self, -1, "Apply and Add a New one")
        self.Bind(wx.EVT_BUTTON, self.OnApply, applyBtn)
                
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(topLbl, 0, wx.ALL, 5)
        mainSizer.Add(wx.StaticLine(self), 0,
                wx.EXPAND|wx.TOP|wx.BOTTOM, 5)
        
        infoSizer = wx.GridBagSizer(hgap=5, vgap=5)

        infoSizer.Add(nameLbl, pos=(0,0), flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(name, pos=(0,1), span=(1, 3), flag=wx.EXPAND)
        
        infoSizer.Add(statusLbl, pos=(1,0), flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        boxSizer = wx.BoxSizer(wx.HORIZONTAL)
        boxSizer.Add(status, proportion=1, flag=wx.EXPAND)
        boxSizer.Add((10,10))
        boxSizer.Add(budgetLbl, flag=wx.ALIGN_CENTER_VERTICAL)
        boxSizer.Add(budget, proportion=1, flag=wx.EXPAND)
        boxSizer.Add(budgetSpb, flag=wx.EXPAND)
        infoSizer.Add(boxSizer, pos=(1,1), span=(1, 3), flag=wx.EXPAND) 
        
        infoSizer.Add(headlineLbl, pos=(2,0),flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(headline, pos=(2,1), span=(1, 3), flag=wx.EXPAND)
        
        infoSizer.Add(desc1Lbl, pos=(3,0),flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(desc1, pos=(3,1), span=(1, 3), flag=wx.EXPAND)
        
        infoSizer.Add(desc2Lbl, pos=(4,0),flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(desc2, pos=(4,1), span=(1, 3), flag=wx.EXPAND)

        infoSizer.Add(display_urlLbl, pos=(5,0),flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(display_url, pos=(5,1), span=(1, 3), flag=wx.EXPAND)
        
        infoSizer.Add(destination_urlLbl, pos=(6,0),flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(destination_url, pos=(6,1), span=(1, 3), flag=wx.EXPAND)
        
        infoSizer.Add(keywordsLbl, pos=(7,0), flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(keyword, pos=(7,1), span=(1,6), flag=wx.EXPAND)
        
        infoSizer.Add(nega_keywordsLbl, pos=(8,0), flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(nega_keyword, pos=(8,1), span=(1,6), flag=wx.EXPAND)
        
        sbox = wx.StaticBox(self, id=-1, label='Ad Preview')
        sboxSizer = wx.StaticBoxSizer(sbox, wx.VERTICAL)
        sboxSizer.Add(preview, proportion=1, flag=wx.EXPAND)
        
        infoSizer.Add(sboxSizer, pos=(0,4), span=(7,3),flag=wx.EXPAND)
        
        infoSizer.AddGrowableCol(1, proportion=2)
        infoSizer.AddGrowableCol(4, proportion=3)
        
        mainSizer.Add(infoSizer, 0, wx.EXPAND|wx.ALL, 10)
        
        mainSizer.Add(wx.StaticLine(self), 0,
                wx.EXPAND|wx.TOP|wx.BOTTOM, 5)
        
        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
        btnSizer.Add((40,40), 1)
        btnSizer.Add(applyBtn)
        btnSizer.Add((10,10), 0)
        btnSizer.Add(applyaddBtn)
        btnSizer.Add((10,10), 0)
        btnSizer.Add(cancelBtn)
        btnSizer.Add((10,10), 0)
        mainSizer.Add(btnSizer, 0, wx.EXPAND|wx.BOTTOM, 10)
        
        self.SetSizer(mainSizer)
        mainSizer.Fit(self)
        mainSizer.SetSizeHints(self)
        
    def OnSpin(self, event):
        self.budget.SetValue(str(event.GetPosition()))
    
    def OnText(self, event):
    
        h = self.headline.GetValue()
        d1 = self.desc1.GetValue() 
        d2 = self.desc2.GetValue()
        u1 = self.display_url.GetValue() 
        u2 = self.destination_url.GetValue()
        self.preview.SetPage(preview_html(h,d1,d2,u1,u2))

    def OnApply(self, event):
        
        self.ad.name = unicode(self.name.GetValue())

        self.ad.budget = decimal.Decimal( self.budget.GetValue() )
        self.ad.status = unicode(status_list[self.status.GetCurrentSelection()])
        
        self.ad.last_modified_at = datetime.datetime.now()
        
        self.ad.headline = unicode(self.headline.GetValue())
        self.ad.desc_line1 = unicode(self.desc1.GetValue())
        self.ad.desc_line2 = unicode(self.desc2.GetValue())
        self.ad.display_url = unicode(self.display_url.GetValue())
        self.ad.destination_url = unicode(self.destination_url.GetValue())
        
        '''
        要改进添加keywords方式 
        '''
        keys = [ Keyword(unicode(name)) for name in self.keyword.GetValue().split(',') if name]
        nega_keys = [ Keyword(unicode(name), is_negative=u'y') for name in self.nega_keyword.GetValue().split(',') if name ]
        self.ad.keywords.clear()
        for key in keys + nega_keys:
            self.ad.keywords.add(key)
               
        env.current_camp.ads.add(self.ad)
        env.current_user.ads.add(self.ad)
        
        env.store.add(self.ad)
        env.store.flush()
        
        ps.Publisher.sendMessage('Ad.ChangeApplied', self.ad)
        
    def SetContent(self, a):
        self.ad = a
        
        self.name.SetValue(a.name)
        
        self.budget.SetValue(str(a.budget))
        self.budgetSpb.SetValue(a.budget)
        
        n = status_list.index(a.status)
        self.status.SetSelection(n)
        
        self.headline.SetValue(a.headline)
        self.desc1.SetValue(a.desc_line1)
        self.desc2.SetValue(a.desc_line2)
        self.display_url.SetValue(a.display_url)
        self.destination_url.SetValue(a.destination_url)
        self.preview.SetPage(a.toHtml())
        
        self.keyword.SetValue(a.keywords2str())
        self.nega_keyword.SetValue(a.keywords2str(type_='y'))
        
if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = wx.Frame(None, size=(680, 400))
    AdPanel(parent=frame, ad=env.store.find(Ad).any())
    frame.Show()
    app.MainLoop()
        
        
